package com.vip.jwt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vip.jwt.common.entity.R;
import com.vip.jwt.common.utils.menu.Menu;
import com.vip.jwt.entity.SystemMenu;
import com.vip.jwt.service.SystemMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author vip
 * @since 2020-06-06
 */
@RestController
@RequestMapping("/api/system-menu")
public class SystemMenuController {

    @Resource
    private SystemMenuService systemMenuService;

    @ApiOperation("添加菜单")
    @PostMapping
    public Object add(@RequestBody SystemMenu menu){
        systemMenuService.save(menu);
        return R.ok();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/{id}")
    public Object del(@PathVariable String id){
        QueryWrapper<SystemMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<SystemMenu> list = systemMenuService.list(wrapper);
        if(list.size()>0){
            return R.error().message("请先删除子节点");
        }
        systemMenuService.removeById(id);
        return R.ok();
    }

    @ApiOperation("修改菜单")
    @PutMapping
    public Object update(@RequestBody SystemMenu menu){
        systemMenuService.updateById(menu);
        return R.ok();
    }

    @ApiOperation("获取菜单树")
    @GetMapping
    public Object tree(){
        return R.ok().data("items",systemMenuService.tree());
    }

    @ApiOperation("获取菜单列表（分页）")
    @GetMapping("/{page}/{limit}")
    public Object pageList(@PathVariable Long page,@PathVariable Long limit){
        Page<SystemMenu> menuPage = new Page<>(page,limit);
        Page<SystemMenu> menus = systemMenuService.page(menuPage);
        return R.ok().data("total",menus.getTotal()).data("items",menus.getRecords());
    }

}

